//
//  ViewController.m
//  tomCat
//
//  Created by dllo on 16/6/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "MyButton.h"

@interface ViewController ()

@property (nonatomic, retain) UIImageView *animationImageView;

@end

@implementation ViewController

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self callMethod];
}

/**调用方法*/
-(void)callMethod
{
    [self createBackground];
    [self cymbalButton];
    [self drinkButton];
    [self eatButton];
    [self pieButton];
    [self fartButton];
    [self scratchButton];
    [self knockoutButton];
    [self footLeftButton];
}

/**创建背景图片*/
-(void)createBackground
{
    self.animationImageView = [[UIImageView alloc]initWithFrame:self.view.frame];
    self.animationImageView.image = [UIImage imageNamed:@"cymbal_00.jpg"];
    [self.view addSubview:self.animationImageView];
    [self.animationImageView release];
    
}


#pragma mark - cymbalButton
-(void)cymbalButton
{    
    MyButton *cymbalButton = [[MyButton alloc]initWithFrame:CGRectMake(30, 400, 50, 50) withSource:@"cymbal" withType:@"png"];
    [self.view addSubview:cymbalButton];
    [cymbalButton release];
    [cymbalButton addTarget:self action:@selector(cymbalAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - drinkButton
-(void)drinkButton
{
    MyButton *drinkButton = [[MyButton alloc]initWithFrame:CGRectMake(30, 400 + 100, 50, 50) withSource:@"drink" withType:@"png"];
    [self.view addSubview:drinkButton];
    [drinkButton release];
    [drinkButton addTarget:self action:@selector(drinkAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - eatButton
-(void)eatButton
{
    MyButton *eatButton = [[MyButton alloc]initWithFrame:CGRectMake(30, 400 + 200, 50, 50) withSource:@"eat" withType:@"png"];
    [self.view addSubview:eatButton];
    [eatButton release];
    [eatButton addTarget:self action:@selector(eatAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - pieButton
-(void)pieButton
{
    MyButton *pieButton = [[MyButton alloc]initWithFrame:CGRectMake(330, 400, 50, 50) withSource:@"pie" withType:@"png"];
    [self.view addSubview:pieButton];
    [pieButton release];
    [pieButton addTarget:self action:@selector(pieAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - fartButton
-(void)fartButton
{
    MyButton *fartButton = [[MyButton alloc]initWithFrame:CGRectMake(330, 400 + 100, 50, 50) withSource:@"fart" withType:@"png"];
    [self.view addSubview:fartButton];
    [fartButton release];
    [fartButton addTarget:self action:@selector(fartAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - scratchButton
-(void)scratchButton
{
    MyButton *scratchButton = [[MyButton alloc]initWithFrame:CGRectMake(330, 400 + 200, 50, 50) withSource:@"scratch" withType:@"png"];
    [self.view addSubview:scratchButton];
    [scratchButton release];
    [scratchButton addTarget:self action:@selector(scratchAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - knockoutButton
-(void)knockoutButton
{
    MyButton *knockoutButton = [[MyButton alloc]initWithFrame:CGRectMake(80, 100, 250, 250) withSource:nil withType:nil];
    [self.view addSubview:knockoutButton];
    [knockoutButton release];
    [knockoutButton addTarget:self action:@selector(knockoutAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - footRightButton
-(void)footRightButton
{
    MyButton *footRightButton = [[MyButton alloc]initWithFrame:CGRectMake(150, 660, 50, 50) withSource:nil withType:nil];
    [self.view addSubview:footRightButton];
    [footRightButton release];
    [footRightButton addTarget:self action:@selector(footRightAction:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - footRightButton方法
-(void)footRightAction:(UIButton *)footRightButton
{
    [self runAnimationWithNum:30 withName:@"footRight"];
}

#pragma mark - footLeftButton
-(void)footLeftButton
{
    MyButton *footLeftButton = [[MyButton alloc]initWithFrame:CGRectMake(220, 660, 50, 50) withSource:nil withType:nil];
    [self.view addSubview:footLeftButton];
    [footLeftButton release];
    [footLeftButton addTarget:self action:@selector(footRightAction:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - footLeftButton方法
-(void)footLeftAction:(UIButton *)footLeftButton
{
    [self runAnimationWithNum:30 withName:@"footLeft"];
}

#pragma mark - knockoutButton方法
-(void)knockoutAction:(UIButton *)knockoutButton
{
    [self runAnimationWithNum:81 withName:@"knockout"];
}

#pragma mark - scratch方法
-(void)scratchAction:(UIButton *)scratchButton
{
    [self runAnimationWithNum:56 withName:@"scratch"];
}

#pragma mark - fart方法
-(void)fartAction:(UIButton *)fartButton
{
    [self runAnimationWithNum:28 withName:@"fart"];
}

#pragma mark - pie方法
-(void)pieAction:(UIButton *)pieButton
{
    [self runAnimationWithNum:24 withName:@"pie"];
}

#pragma mark - eat方法
-(void)eatAction:(UIButton *)eatButton
{
    [self runAnimationWithNum:40 withName:@"eat"];
}

#pragma mark - drink方法
-(void)drinkAction:(UIButton *)drinkButton
{
    [self runAnimationWithNum:81 withName:@"drink"];
}

#pragma mark - cymbal方法
-(void)cymbalAction:(UIButton *)cymbalButton
{
    [self runAnimationWithNum:13 withName:@"cymbal"];
}

#pragma mark - 动画效果
-(void)runAnimationWithNum:(NSInteger)count withName:(NSString *)name
{
    if ([self.animationImageView isAnimating]) {
        return;
    }
    
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        NSString *newName = [NSString stringWithFormat:@"%@_%02d.jpg",name, i];
        UIImage *image = [UIImage imageNamed:newName];
        [images addObject:image];
    }
    self.animationImageView.animationImages = images;
    self.animationImageView.animationRepeatCount = 1;
    self.animationImageView.animationDuration = count * 0.05;
    [self.animationImageView startAnimating];
}


@end











